package com.qf.bigdata.view.web.service.impl;

import com.qf.bigdata.view.web.service.DeviceService;
import com.qf.data.view.core.model.request.AddDeviceRequest;
import com.qf.data.view.core.model.request.UpdateDeviceRequest;
import com.qf.data.view.core.model.response.DeviceSelectResponse;
import com.qf.data.view.core.model.result.ResultModel;
import com.qf.data.view.facade.api.DeviceFacade;
import com.qf.data.view.facade.request.device.DeviceModelRequest;
import com.qf.data.view.facade.response.device.DeviceModelResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author q
 * @version V1.0
 * @date 2022/01/04
 **/
@Service
public class DeviceServiceImpl implements DeviceService {

    @Reference
    private DeviceFacade deviceFacade;

    @Override
    public ResultModel<DeviceSelectResponse> getDeviceByKey(String key) {

        DeviceModelRequest deviceModelRequest = new DeviceModelRequest();
        deviceModelRequest.setDeviceKey(key);
        //使用dubbo进行远程过程调用
        DeviceModelResponse deviceModelResponse = deviceFacade.getDeviceByKey(deviceModelRequest);
        //结果进行解析
        DeviceSelectResponse deviceSelectResponse = new DeviceSelectResponse();
        if (Objects.nonNull(deviceModelResponse)){
            BeanUtils.copyProperties(deviceModelResponse, deviceSelectResponse);
        }
        return ResultModel.success(deviceSelectResponse);
    }

    @Override
    public ResultModel addDevice(AddDeviceRequest addDeviceRequest) {
        DeviceModelRequest deviceModelRequest = new DeviceModelRequest();
        BeanUtils.copyProperties(addDeviceRequest, deviceModelRequest);
        Integer row = deviceFacade.addDevice(deviceModelRequest);
        if (row==1){
            return ResultModel.success("添加成功~");
        }
        return ResultModel.error("添加失败");
    }

    @Override
    public ResultModel updateByDeviceId(UpdateDeviceRequest updateDeviceRequest) {
        DeviceModelRequest deviceModelRequest = new DeviceModelRequest();
        BeanUtils.copyProperties(updateDeviceRequest, deviceModelRequest);
        Integer row = deviceFacade.updateByDeviceId(deviceModelRequest);
        if (row==1){
            return ResultModel.success("修改成功~");
        }
        return ResultModel.error("修改失败");
    }
}
